/*-------------------------------------
		sPlugin
	    By Scooby, 2010
-------------------------------------*/

local PLUGIN = {}

PLUGIN.Name = "Ragdoll"
PLUGIN.Author = "Scooby"
PLUGIN.Command = "ragdoll"
PLUGIN.Group = 2
PLUGIN.Category = "Punishment"
PLUGIN.Format = "string"
PLUGIN.Use = "<Player> <1/0>"
PLUGIN.Menu = {
	{
		title = "Choose Player",
		cont = "#players",
		apos = 1
	}
}
local info = {}
info.ent = {}
info.weps = {}
info.sel = {}

function PLUGIN:Main( ply, args )
	
	local on = tonumber( args[2] ) or 1
	local pl = spl:FindPly( args[1] )
	if( #pl == 0 ) then
		spl:PrintC( ply, spl.red, spl.fnp )
		return false
	elseif( #pl > 1 ) then
		spl:PrintC( ply, spl.red, spl.fmp )
		return false
	end
		
	if( !info[ pl[1]:SteamID() ] ) then info[ pl[1]:SteamID() ] = false end	
		
	if( on == 1 ) then

		if( info[ pl[1]:SteamID() ] == false ) then
			info.ent[ pl[1]:SteamID() ] = ents.Create( "prop_ragdoll" )
			info.ent[ pl[1]:SteamID() ]:SetPos( pl[1]:GetPos() )
			info.ent[ pl[1]:SteamID() ]:SetModel( pl[1]:GetModel() )
			info.ent[ pl[1]:SteamID() ]:SetAngles( pl[1]:GetAngles() )
			info.ent[ pl[1]:SteamID() ]:Spawn()
			
			info.weps[ pl[1]:SteamID() ] = {} -- To Keep The Weapons They Had
			for _,v in ipairs( pl[1]:GetWeapons() ) do
				table.insert( info.weps[ pl[1]:SteamID() ], v:GetClass() )
			end
			info.sel[ pl[1]:SteamID() ] = pl[1]:GetActiveWeapon():GetClass()
			
			pl[1]:Spectate( OBS_MODE_CHASE )
			pl[1]:SpectateEntity( info.ent[ pl[1]:SteamID() ] )
			pl[1]:SetMoveType( MOVETYPE_OBSERVER )
			pl[1]:StripWeapons()
	
			hook.Add( "PlayerLoadout", "Admin_ragdoll"..pl[1]:SteamID(), function( plys )
				if( pl[1] == plys ) then
					plys:Spectate( OBS_MODE_CHASE )
					plys:SpectateEntity( info.ent[ pl[1]:SteamID() ] )
					plys:SetMoveType( MOVETYPE_OBSERVER )
					plys:StripWeapons()
					return false
				end
			end)
			
			hook.Add( "PlayerSpawnObject", "Admin_object"..pl[1]:SteamID(), function( plyss )
				if( pl[1] == plyss ) then
					return false
				end
			end)
			
			info[ pl[1]:SteamID() ] = true
			
			spl:PrintC( spl.red, pl[1]:Nick(),spl.white,  " Has Been Ragdoled By ", spl.blue, ply:Nick() )
		else
			spl:PrintC( ply, spl.red, "That Player Is Already Ragdolled" )
		end
	
	elseif( on == 0 ) then
		if( info[ pl[1]:SteamID() ] == true ) then
		
			hook.Remove( "PlayerLoadout", "Admin_ragdoll"..pl[1]:SteamID() )
			hook.Remove( "PlayerSpawnObject", "Admin_object"..pl[1]:SteamID() )
			
			pl[1]:UnSpectate()
			local p = pl[1]:GetPos()
			local a = pl[1]:GetAngles()
			pl[1]:Respawn()
			pl[1]:SetPos( p )
			pl[1]:SetAngles( a )
			info[ pl[1]:SteamID() ] = false
			info.ent[ pl[1]:SteamID() ]:Remove()
			
			for _,v in ipairs( info.weps[ pl[1]:SteamID() ] ) do
				pl[1]:Give( v )
			end
			pl[1]:SelectWeapon( info.sel[ pl[1]:SteamID() ] )
			
			spl:PrintC( spl.green, pl[1]:Nick(), spl.white, " Was Unragdolled By ", spl.blue, ply:Nick() )
		else
			spl:PrintC( ply, spl.red,"That Player Is Not Ragdolled" )
		end
		
	end
	
end

spl:AddPlugin( PLUGIN )